<?php
$head_photo_url = empty($model->picture) ? Yii::app()->baseUrl . '/img/user-128.png' : $model->picture;
?>
<div class="row-fluid">
	<div class="span4">
		<div class="row-fluid">
			<div class="span12 text-center">
				<p>
					<img id="user-picture" class="img-polaroid" src="<?php echo $head_photo_url; ?>" style="width:50px;" />
				</p>
				<h4 style="font-weight:normal;" class="hidden">
					No: <strong><?php echo Yii::app()->user->id; ?></strong>
				</h4>
			</div>
			<div class="span12 text-center hide">
				<a class="btn btn-primary"><?php echo Yii::t('app', 'Upload picture'); ?></a>
			</div>
		</div>
<?php if (!isset(Yii::app()->user->facebook) or Yii::app()->user->facebook == 0) { ?>
		<div id="facebook-connection-box">
			<hr />
			<div class="row-fluid">
				<div class="span12 text-center">
					<p class="text-left"><?php echo Yii::t('app', 'Connect to your facebook to fill up your basic information.'); ?></p>
					<a class="facebookButton facebook-connect"><?php echo Yii::t('app', 'Connect to Facebook'); ?></a>
				</div>
			</div>
		</div>
		<div id="connected-facebook" class="hide">
			<p>You have connected Facebook!</p>
			<span style="font-size:0.8em;">We don't post anything on your wall without your permission.</span>
		</div>
<?php } ?>
	</div>
	<div class="span8">
<?php $form = $this->beginWidget('CActiveForm', array(
	'id'=>'user-form',
	'enableAjaxValidation'=>true,
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>
	<legend><?php echo Yii::t('app', 'Basic inforamtion'); ?></legend>
	<label class="control-label"><?php echo $form->labelEx($model,'name'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->textField($model, 'name', array('id'=>'user-name', 'class'=>'span6')); ?>
	</div>
	<div class="space"></div>
	<label class="control-label"><?php echo $form->labelEx($model,'mobile'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->textField($model, 'mobile', array('id'=>'user-mobile', 'class'=>'span6')); ?>
	</div>
	<div class="space"></div>
	<label class="control-label"><?php echo $form->labelEx($model,'email'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->textField($model, 'email', array('id'=>'user-email', 'class'=>'span6')); ?>
	</div>
	<div class="space"></div>
	<label class="control-label"><?php echo $form->labelEx($model,'gender'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->dropDownList($model, 'gender', array(
			'' => '',
			'female' => Yii::t('app', 'Female'),
			'male' => Yii::t('app', 'Male'),
		), array('id'=>'user-gender')); ?>
	</div>
	<div class="space"></div>
	<label class="control-label"><?php echo $form->labelEx($model,'lang'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->dropDownList($model, 'lang', array(
			'en_gb' => 'English',
			'fi_fi' => 'Finnish',
		), array('id'=>'user-lang')); ?>
	</div>
	<hr />
	<div class="errorMessage"></div>
	<button class="btn btn-large btn-primary" id="update-settings"><?php echo BUtils::T('update_lbl'); ?></button>
<?php $this->endWidget(); ?>
	</div>
</div>
<script>
$(function() {
	//facebook connect
	$('.facebook-connect').click(function(){
		fbUI.connect('<?php echo Yii::app()->createUrl('/user/facebook'); ?>', function(data){
			if (data.code === 200) {
				if ($('#user-name').val() == '') $('#user-name').val(data.message.name);
				if ($('#user-email').val() == '') $('#user-email').val(data.message.email);
				if ($('#user-gender').val() == '') $('#user-gender').val(data.message.gender);
				if ($('#user-lang').val() == '') $('#user-lang').val(data.message.lang);
				$('#facebook-connection-box').hide();
				$('#connected-facebook').show();
			}
		});
	});
	//save settings
	$('#update-settings').click(function(ev){
		ev.preventDefault();
		var _this = $(this);
		var _form = $(this).parent('form');
		$.ajax({
			url: _form.attr('action'), data: _form.serialize(), type: 'POST',
			beforeSend: function() {
				_this.attr('disabled', true);
				$('.errorMessage').html('');
			},
			error: function(jqXHR) {
				_this.attr('disabled', false);
				$('.errorMessage').html(jqXHR.responseText);
			},
			success: function(html) {
				_this.attr('disabled', false);
				alert(html);
			}
		});
	});
});
</script>